package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.school.entity.CourseSign;
import cn.stylefeng.guns.modular.school.mapper.CourseSignMapper;
import cn.stylefeng.guns.modular.school.model.params.CourseSignParam;
import cn.stylefeng.guns.modular.school.model.result.CourseSignResult;
import cn.stylefeng.guns.modular.school.service.CourseSignService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 签到信息 服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-03
 */
@Service
public class CourseSignServiceImpl extends ServiceImpl<CourseSignMapper, CourseSign> implements CourseSignService {

    @Override
    public void add(CourseSignParam param) {
        CourseSign entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CourseSignParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CourseSignParam param) {
        CourseSign oldEntity = getOldEntity(param);
        CourseSign newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CourseSignResult findBySpec(CourseSignParam param) {
        return null;
    }

    @Override
    public List<CourseSignResult> findListBySpec(CourseSignParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CourseSignParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<CourseSignResult> records = page.getRecords();
        records.forEach(courseSignResult -> {
            courseSignResult.setLessonDate(DateUtils.getDayBaseOnDate(courseSignResult.getSignDate(), (courseSignResult.getWeek() + 1) == 8 ? 1 : courseSignResult.getWeek() + 1));
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CourseSignParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CourseSign getOldEntity(CourseSignParam param) {
        return this.getById(getKey(param));
    }

    private CourseSign getEntity(CourseSignParam param) {
        CourseSign entity = new CourseSign();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
